package com.example.notification;

import com.example.client.NotificationRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public record NotificationConsumer(NotificationService notificationService) {

    @RabbitListener(queues = "${rabbitmq.queues.notification}")
    public void consumer(NotificationRequest notificationRequest) {
        log.info("接收到来自消息队列的通知请求: {}", notificationRequest);

        try {
            notificationService.notifyCustomer(notificationRequest);
            log.info("通知处理完成: {}", notificationRequest.toCustomerEmail());
        } catch (Exception e) {
            log.error("处理通知失败: {}", e.getMessage(), e);
            // 这里可以添加重试逻辑或者将消息放入死信队列
        }
    }
}